define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'article/article/index' + location.search,
                    add_url: 'article/article/add',
                    edit_url: 'article/article/edit',
                    del_url: 'article/article/del',
                    multi_url: 'article/article/multi',
                    import_url: 'article/article/import',
                    table: 'article',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                searchFormVisible:false,
                visible: false,
                showToggle: false,
                showColumns: false,
                showExport:false,
           
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                      //  {field: 'category_id', title: __('Category_id')},
                    //   {field: 'category_id', title: __('Category_id'),searchList:$.getJSON("articAle/categorydate")},
                //       {field: 'category_id', title: __('category_id'),searchList: $.getJSON("article/article/categorydate"),operate: '=',formatter: function (value,row) {
                //         if(row.hasOwnProperty("category")&&row.category){
                //             if(row.category.hasOwnProperty("name")&&row.category.name){
                //                 return row.category.name;
                //             }
                //         }
                // }
                // },
                {field: 'category.name', title: __('category_id'), operate: '='},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        // {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'views', title: __('Views')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        // {field: 'switch', title: __('Switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        // {field: 'ztsh', title: __('ztsh'), searchList: {"0":__('ztsh 0'),"1":__('ztsh 1'),"2":__('ztsh 2')}, formatter: Table.api.formatter.status},
                         {field: 'buttons',width: "120px",title: __('查看'),table: table,events: Table.api.events.operate,buttons: [
                          

                            {name: 'addtabs',text: __('查看详细'),title: __('查看详细'),classname: 'btn btn-xs btn-warning btn-addtabs',icon: 'fa fa-folder-o',
                                url: 'dashboard/detail?cid={$row.category_id}'
                            }
                        ],
                            formatter: Table.api.formatter.buttons
                        },
                        
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'article/article/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'article/article/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'article/article/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
           
        },
        edit: function () {
            Controller.api.bindevent();
        },
        rebut: function () {
            $(document).on("click", "#tijiao", function () {
                $.post("article/article/rebut",{'id':$('#c-id').val(),'remarks':$('#c-remarks').val()},function(data){
                    console.log(data);
                    if(data.code == 1){
                        Toastr.success(data.msg);
                      
                        parent.location.reload();

                    }else{
                        Toastr.error(data.msg);
                        return false;
                    }
                });
            })


        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});